package qing.controller.cms;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import qing.common.des;
import qing.entity.Person;
import qing.service.personService;

@Controller
@RequestMapping("/cms")
public class AdminController {

	@Autowired
	private personService personService;	
	

	@RequestMapping("userlist")
	public String showuserlists(Map<String, Object> map) {
		System.out.println("显示list:");
		map.put("users", personService.list("from Person"));
		return "userlist";
	}
	
	//绑定会员模型
	@ModelAttribute
	public void getUser(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
		if (id != null) {
			System.out.println("modeAttribute");
			map.put("user", personService.find(Person.class, id));
		}
	}
	
//添加用户显示
	@RequestMapping(value = "adduser", method = RequestMethod.GET)
	public String adduserget(Map<String, Object> map) {
		//map.put("deps", departmentdao.getDepartments());
		map.put("user", new Person());
		return "adduser";
	}
//修改用户
	@RequestMapping(value = "adduser/{id}", method = RequestMethod.GET)
	public String edituser(@PathVariable("id") Integer id, Map<String, Object> map) {
		map.put("user", personService.find(Person.class, id));
		return "adduser";
	}
   //提交保存用户
   //@Valid 属性必须和BindingResult 挨着，中间不能有其他参数隔开 
	@RequestMapping(value = "adduser", method = RequestMethod.POST)
	public String adduserpost(@Valid Person u, BindingResult result, Map<String, Object> map) {		
		System.out.println("验证会员信息");
		if (result.getErrorCount() > 0) {
			// 打印出错误信息，调试用
			for (FieldError error : result.getFieldErrors()) {
				System.out.println(error.getField() + ":" + error.getDefaultMessage());
			}
			// 若验证出错，转向错误页面,初始化页面绑定数据
			map.put("user",u);
			return "adduser";
		}
		personService.create(u);
		return "redirect:userlist";
	}
//提交修改用户
	@RequestMapping(value = "adduser", method = RequestMethod.PUT)
	public String eidtuserpost(@ModelAttribute("user") Person u, BindingResult result, Map<String, Object> map,@RequestParam(value="oldpwd", required=false) String oldpwd ) {
		System.out.println("开始修改，并重定向……" + u.toString());
System.out.println("oldpwd:"+oldpwd);
		if (result.getErrorCount() > 0) {
			// 打印出错误信息，调试用
			for (FieldError error : result.getFieldErrors()) {
				System.out.println(error.getField() + ":" + error.getDefaultMessage());
			}

			// 若验证出错，转向错误页面,初始化页面绑定数据
			map.put("user",u);
			return "adduser";
		}
//验证用户是否更改密码
		if(!oldpwd.equals(u.getPassword()))
			u.setPassword(des.md5(u.getPassword()));		
		personService.update(u);
		return "redirect:userlist";
	}
//删除用户
	@RequestMapping(value = "userdel/{id}", method = RequestMethod.DELETE)
	public String deleteUser(@PathVariable("id") Integer id) {
		System.out.println("dele:" + id);
		personService.delete(Person.class, id);
		return "redirect:../userlist";
	}


	
	
	
	

}
